<template>
	<view class="">
		<view class="form">

			<view class="title">
				<text class="text">密码登录</text>
				<view class="right">
					<slot></slot>
				</view>
			</view>
			<view class="input line">
				<input v-model="user.mobile" placeholder="手机号" type="text" />
			</view>
			<view class="input ">
				<input placeholder="密码" v-model="user.pass"  :password="showPassword" />

				<span @click="changeInputType" :class="showPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
					class="icon iconfont "></span>


			</view>


		</view>
		<button class="btn" @click="submit">登录</button>
	</view>
</template>

<script>
	import {
		loginByPassword
	} from '../../service/member.js'
	 import {getCheckHouse} from '../../service/houses.js'
	export default {
		data() {
			return {
				showPassword: true,
				user: {
					mobile: "",
					pass: ""
				}

			}

		},
		methods: {
			changeInputType() {

				this.showPassword = !this.showPassword;

			},
			async submit() {
				
				uni.showLoading({
					title:'正在登录...'
				})
				const res=await loginByPassword(this.user).catch(()=>undefined);
			
				if(res){
					 uni.hideLoading();
					uni.setStorageSync("token",res.token);
					
					//获取用户房屋信息 并存到本地缓存
					const houseRes=await getCheckHouse();
					if(houseRes && houseRes.list.length>0){
						 uni.setStorageSync("houses",JSON.stringify(houseRes.list));
					}
					
					const url=uni.getStorageSync("url") || '/pages/index/index';
					uni.redirectTo({
						url
					})
					
				}

			}
		},
	}
</script>

<style lang="less">
	@import url('../../static/css/login.less');
</style>